const { dialog, ipcMain } = require('electron');
let fs = require('fs');

ipcMain.handle('on-savefile-event', async (e) => {
    return await dialog.showOpenDialog({ properties: ['openFile'] }).then(async (result) => {
        if (result.canceled) {
            return null;
        }
        let data = await fs.promises.readFile(result.filePaths[0]);
        let lastIndex = result.filePaths[0].lastIndexOf(".");
        let suffix = "";
        if (lastIndex !== -1) {
            suffix = "." + result.filePaths[0].substring(lastIndex + 1);
        } else {
            console.log("err");
            return null;
        }
        let fileName = "" + Date.now() + suffix;
        await fs.promises.writeFile("./public/static/" + fileName, data);
        return fileName;
    });
});